<template>
  <div class="member-order-page">
    <XtxTabs v-model="activeName" @click-tab="clickTab">
      <XtxTabsPanel name="all" label="全部订单"></XtxTabsPanel>
      <XtxTabsPanel name="unpay" label="待付款"></XtxTabsPanel>
      <XtxTabsPanel name="deliver" label="待发货"></XtxTabsPanel>
      <XtxTabsPanel name="receive" label="待收货"></XtxTabsPanel>
      <XtxTabsPanel name="comment" label="待评价"></XtxTabsPanel>
      <XtxTabsPanel name="complete" label="已完成"></XtxTabsPanel>
      <XtxTabsPanel name="cancel" label="已取消"></XtxTabsPanel>
    </XtxTabs>
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
        v-for="item in orderList"
        :key="item.id"
        :order="item"
        @on-cancel-order="onCancelOrder(item)"
        @on-delete-order="onDeleteOrder(item)"
        @on-confirm-order="onConfirmOrder(item)"
      />
      <XtxPagination
        v-if="total > requestParams.pageSize"
        @current-change="requestParams.page = $event"
        :total="total"
        :page-size="requestParams.pageSize"
        :current-page="requestParams.page"
      />
    </div>
    <!-- 对于像：对话框，消息提示，确认提示，通知组件 适合使用传送门 Teleport -->
    <Teleport to="#dailog">
      <OrderCancel ref="orderCancelCom" />
    </Teleport>
    <!-- 查看物理组件 -->
    <Teleport to="#dailog">
      <OrderLogistics ref="logisticsOrderCom" />
    </Teleport>
    <!-- <div v-else class="loading"></div> -->
  </div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import { findOrderList, delteOrder, confirmOrder } from '@/api/order'
import OrderItem from './components/order-item'
import OrderCancel from './components/order-cancel.vue'
import Confirm from '@/components/library/confirm'
import Message from '@/components/library/Message'
import OrderLogistics from './components/order-logistics'

export default {
  name: 'MemberOrder',
  components: { OrderItem, OrderCancel, OrderLogistics },
  setup() {
    const activeName = ref('all')
    const clickTab = (...arg) => {
      // 此时：index 就是订单的状态
      requestParams.orderState = arg[0].index
      requestParams.page = 1
    }

    // 查询订单参数
    const requestParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0,
    })
    // 订单列表
    const orderList = ref([])
    const loading = ref(false)
    const total = ref(0)
    // 初始化后，查询条件更改后，查询。
    // 使用侦听器，监听 requestParams 的改变
    const findOrderListFn = () => {
      loading.value = true
      findOrderList(requestParams).then((data) => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }
    watch(
      requestParams,
      () => {
        findOrderListFn()
      },
      { immediate: true }
    )
    // 查询订单
    findOrderList(requestParams).then((data) => {
      orderList.value = data.result.items
    })

    // 封装逻辑-取消订单
    const useCancelOrder = () => {
      const orderCancelCom = ref(null) //绑定对话框组件
      const onCancelOrder = (item) => {
        // item 就是你要取消的订单
        orderCancelCom.value.open(item)
      }
      return { onCancelOrder, orderCancelCom }
    }
    // 删除订单
    const onDeleteOrder = (item) => {
      Confirm({ text: '您确认删除该条订单吗？' })
        .then(() => {
          delteOrder([item.id]).then(() => {
            Message({ text: '删除订单成功', type: 'success' })
            findOrderListFn()
          })
        })
        .catch((e) => {})
    }

    // 封装逻辑-确认收货
    const useConfirmOrder = () => {
      const onConfirmOrder = (item) => {
        // item 就是你要确认收货的订单
        Confirm({ text: '您确认收到货吗？确认后货款将会打给卖家。' }).then(
          () => {
            confirmOrder(item.id).then(() => {
              Message({ text: '确认收货成功', type: 'success' })
              // 确认收货后状态变成 待评价
              item.orderState = 4
            })
          }
        )
      }
      return { onConfirmOrder }
    }

    // 封装逻辑-查看物流
    const useLogisticsOrder = () => {
      const logisticsOrderCom = ref(null) //绑定物流组件
      const onLogisticsOrder = (item) => {
        logisticsOrderCom.value.open(item)
      }
      return { onLogisticsOrder, logisticsOrderCom }
    }

    return {
      activeName,
      clickTab,
      orderList,
      loading,
      total,
      requestParams,
      ...useCancelOrder(),
      onDeleteOrder,
      ...useConfirmOrder(),
      ...useLogisticsOrder(),
    }
  },
}
// 取消订单逻辑
export const useCancelOrder = () => {
  // 组件实例
  const orderCancelCom = ref(null)
  // 点击取消
  const handlerCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}
// 确认收货逻辑
export const useConfirmOrder = () => {
  const handlerConfirm = (order) => {
    Confirm({ text: '亲，您确认收货吗？确认后货款将打给卖家。' })
      .then(() => {
        confirmOrder(order.id).then(() => {
          Message({ type: 'success', text: '确认收货成功' })
          // 待收货--->待评价
          order.orderState = 4
        })
      })
      .catch(() => {})
  }
  return { handlerConfirm }
}
// 查看物流逻辑
export const useLogisticsOrder = () => {
  const orderLogisticsCom = ref(null)
  const handlerLogistics = (order) => {
    orderLogisticsCom.value.open(order)
  }
  return { handlerLogistics, orderLogisticsCom }
}
</script>
<style scoped lang="less">
.order-list {
  background: #fff;
  padding: 20px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;
  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;
    span {
      margin-right: 20px;
      &.down-time {
        margin-right: 0;
        float: right;
        i {
          vertical-align: middle;
          margin-right: 3px;
        }
        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }
    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }
  .body {
    display: flex;
    align-items: stretch;
    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;
      > p {
        padding-top: 10px;
      }
      &:first-child {
        border-left: none;
      }
      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;
        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;
            &:last-child {
              border-bottom: none;
            }
            .image {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }
            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;
              p {
                margin-bottom: 5px;
                &.name {
                  height: 38px;
                }
                &.attr {
                  color: #999;
                  font-size: 12px;
                  span {
                    margin-right: 5px;
                  }
                }
              }
            }
            .price {
              width: 100px;
            }
            .count {
              width: 80px;
            }
          }
        }
      }
      &.state {
        width: 120px;
        .green {
          color: @xtxColor;
        }
      }
      &.amount {
        width: 200px;
        .red {
          color: @priceColor;
        }
      }
      &.action {
        width: 140px;
        a {
          display: block;
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
</style>

